package com.sven.sharedb.range.rest;

import com.sven.sharedb.core.message.ObjectRestResponse;
import com.sven.sharedb.range.entity.dto.UpdateByExampleDTO;
import com.sven.sharedb.range.service.IShareDbService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @author sven.wang
 * @version 2022/4/26 16:55
 */
@RestController
@RequestMapping("sharedb")
public class ShareDbController {

    @Resource
    private IShareDbService iShareDbService;

    @PostMapping("/webTest")
    public ObjectRestResponse<String> webTest() {
        return ObjectRestResponse.success("Spring Boot web test");
    }

    @GetMapping("/selectBySql")
    public ObjectRestResponse<String> selectBySql(@RequestParam(value = "orderId",required = false) String orderId,
                                                  @RequestParam(value = "createDate",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createDate,
                                                  @RequestParam(value = "beginDate",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
                                                  @RequestParam(value = "endDate",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        return ObjectRestResponse.success(iShareDbService.selectBySql(orderId, createDate, beginDate, endDate));
    }

    @GetMapping("/selectByExample")
    public ObjectRestResponse<String> selectByExample(@RequestParam(value = "orderId",required = false) String orderId,
                                                      @RequestParam(value = "createDate",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createDate,
                                                      @RequestParam(value = "beginDate",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
                                                      @RequestParam(value = "endDate",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        return ObjectRestResponse.success(iShareDbService.selectByExample(orderId, createDate, beginDate, endDate));
    }

    @PostMapping("/updateBySql")
    public ObjectRestResponse<Integer> updateBySql(@RequestBody UpdateByExampleDTO dto) {
        return ObjectRestResponse.success(iShareDbService.updateBySql(dto));
    }

    @PostMapping("/updateByExample")
    public ObjectRestResponse<Integer> updateByExample(@RequestBody UpdateByExampleDTO dto) {
        return ObjectRestResponse.success(iShareDbService.updateByExample(dto));
    }

    @PostMapping("/deleteBySql")
    public ObjectRestResponse<Integer> deleteBySql(@RequestBody UpdateByExampleDTO dto) {
        return ObjectRestResponse.success(iShareDbService.deleteBySql(dto));
    }

    @PostMapping("/deleteByExample")
    public ObjectRestResponse<Integer> deleteByExample(@RequestBody UpdateByExampleDTO dto) {
        return ObjectRestResponse.success(iShareDbService.deleteByExample(dto));
    }

    @GetMapping("/selectJoinDetailBySql")
    public ObjectRestResponse<String> selectJoinDetailBySql(@RequestParam(value = "orderId",required = false) String orderId,
                                                  @RequestParam(value = "createDate",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createDate,
                                                  @RequestParam(value = "beginDate",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
                                                  @RequestParam(value = "endDate",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        return ObjectRestResponse.success(iShareDbService.selectJoinDetailBySql(orderId, createDate, beginDate, endDate));
    }

}
